import Vue from 'vue';

interface Adapter {
    warpComponentMeta(meta: {
        template
    });
    getBaseClass();
}

function replaceAll(str, oldStr, newStr) {
    str = str.replace(oldStr, newStr);
    if (str.indexOf(oldStr) > -1) {
        return replaceAll(str, oldStr, newStr);
    }
    return str;
}

function transformAngularToVUE(template) {
    let newTemplate = replaceAll(template, "(click)", "@click");
    newTemplate = replaceAll(newTemplate, "[(ngModel)]", "v-model");
    newTemplate = replaceAll(newTemplate, "*ngIf", "v-if");
    return newTemplate;
}

function transformVUEToAngular(template) {
    let newTemplate = replaceAll(template, "@click", "(click)");
    newTemplate = replaceAll(newTemplate, "v-model", "[(ngModel)]");
    newTemplate = replaceAll(newTemplate, "v-if", "*ngIf");
    newTemplate = replaceAll(newTemplate, "router-view", "router-outlet");
    return newTemplate;
}

export class VueAdapter implements Adapter {
    warpComponentMeta(meta: {
        template: any, styleUrls?: Array<string>, styles?:Array<string>
    }) {
        meta.template = transformAngularToVUE(meta.template);

        return meta;
    }
    getBaseClass() {
        return Vue;
    }
}

export class AngularAdapter implements Adapter {
    warpComponentMeta(meta: {
        template: any; styleUrls: string[]
    }) {
        meta.template = transformVUEToAngular(meta.template)
        return meta;
    }
    getBaseClass() {
        return Vue;
    }
}



export const currentFramework: string = "angular";
export const adapter = ((currentFramework == "VUE") ? new VueAdapter() : new AngularAdapter())